package com.kaishengit.controller;

import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaishengit.dto.Message;
import com.kaishengit.pojo.Role;
import com.kaishengit.pojo.User;
import com.kaishengit.service.UserService;

@Controller
public class CurrentUserController {

    @Inject
    private UserService userService;

    /**
     * 跳转到当前用户信息
     * @return
     */
    @RequestMapping(value = "/goUserSet",method = RequestMethod.GET)
    public String goSetUser(HttpServletRequest request) {
    	User user  = (User) request.getSession().getAttribute(User.SESSION_KEY);
    	//查找当前用户对应的角色
    	List<Role> roleList = userService.findRoleByUserId(user.getId());
    	if(!roleList.isEmpty()) {
    		//设置当前用户的角色
    		request.setAttribute("role", roleList.get(0).getRolename());
    	}
    	request.setAttribute("user", user);
        return "currentUser";
    }
    
    /**
     * 修改当前用户信息
     * @return
     */
    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    @ResponseBody
    public String updateUser(RedirectAttributes redirectAttributes,User user) {
    	//查找当前用户对应的角色
    	userService.updateCurrentUser(user);
    	redirectAttributes.addFlashAttribute("message",new Message(Message.SUCCESS,"信息修改成功,请重新登录"));
    	return null;
    }
}
